package controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.xpath.internal.operations.Or;
import model.Order;
import model.OrderDao;
import model.Shop;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends HttpServlet {

    //使用这个数据类型来将数据转成json格式
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");

        //接收客户端发来的json数据
        InputStreamReader reader = new InputStreamReader(req.getInputStream(),"UTF-8");
        char[] buff = new char[1024];
        int length = 0;
        String str = "";
        while((length = reader.read(buff))!=-1){
            str = str + new String(buff,0,length);
        }
        reader.close();


        System.out.println("购买后客户端发来的数据；"+str);

        //分割数据
        String shopStr = str.split("]",2)[0]+"]";
        String userIdStr = str.split("]",2)[1];
        String userIds = userIdStr.replace("\"","");
        int userId = Integer.parseInt(userIds);
        System.out.println("要插入的数据"+shopStr);
        System.out.println("插入数据的数量"+userIds); //  1   2  3  这种类型

        //解析发来的数据
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Shop.class);
        List<Shop> orderShop = objectMapper.readValue(shopStr,javaType);


        //在shoporder 创建一条订单信息,然后获取创建的订单id，再放入用作订单中商品所属的订单
        OrderDao orderDao = new OrderDao();
        int orderId = orderDao.createShopOrder(userId);

        //将新发来的数据存到orderdetail里面    然后更新shoporder的订单总价格
        orderDao.insertShop(orderId,orderShop);

        System.out.println("购买的商品数量为:"+orderShop.size());
        System.out.println(str);


        String respJson = objectMapper.writeValueAsString(str);
        resp.getWriter().write(respJson);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");

        //接收当前用户id
        String userID = req.getParameter("userId");
        int userId = Integer.parseInt(userID);


        //获取shoporder中的所有订单
        OrderDao orderDao = new OrderDao();
        //使用List存储订单信息
        List<Order> orderList = new ArrayList<>();
        orderList = orderDao.selectAllOrder(userId);
        //将orderlist转为json发送回去
        String respJson = objectMapper.writeValueAsString(orderList);
        System.out.println("所有的订单项为"+respJson);
        resp.getWriter().write(respJson);


    }


}















